﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;

namespace CloudClip.HelperMehtod
{
    public static class FileWatcherHelper
    {
        #region WatcherLog
        public static void CreateWatcherLog(string sPath)
        {
            CreateWatcherLog(sPath, "*");
        }

        public static void CreateWatcherLog(string sPath, string sFilter)
        {
            if (string.IsNullOrEmpty(sPath))
                return;

            //Create a new FileSystemWatcher.
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = sPath;
            watcher.Filter = sFilter;

            //Subscribe to the All event. 暫時為了監控事件 先做Log用
            watcher.Changed += new FileSystemEventHandler(WatcherEvent);
            watcher.Deleted += new FileSystemEventHandler(WatcherEvent);
            watcher.Created += new FileSystemEventHandler(WatcherEvent);
            watcher.Renamed += new RenamedEventHandler(WatcherRenamed);

            //Enable the FileSystemWatcher events.
            watcher.EnableRaisingEvents = true;
        }

        private static void WatcherEvent(object sender, FileSystemEventArgs e)
        {
            LoggerHelper.WriteDebugLog(string.Format("[{0}]{1}", e.ChangeType, e.FullPath));
        }

        private static void WatcherRenamed(object sender, RenamedEventArgs e)
        {
            LoggerHelper.WriteDebugLog(string.Format("[{0}]{1} -> {2}", e.ChangeType, e.OldFullPath, e.Name));
        }
        #endregion
      

        public static void CreateWatcher(string sPath, FileSystemEventHandler fileSystemEventHandler)
        {
            CreateWatcher(sPath, "*", fileSystemEventHandler, null);
        }    

        public static void CreateWatcher(string sPath,FileSystemEventHandler fileSystemEventHandler,RenamedEventHandler renamedEventHandler)
        {
            CreateWatcher(sPath, "*", fileSystemEventHandler, renamedEventHandler);
        }

        public static void CreateWatcher(string sPath,string sFilter, FileSystemEventHandler fileSystemEventHandler)
        {
            CreateWatcher(sPath, sFilter, fileSystemEventHandler, null);
        }

        public static void CreateWatcher(string sPath, string sFilter ,FileSystemEventHandler fileSystemEventHandler,RenamedEventHandler renamedEventHandler)
        {
            if (string.IsNullOrEmpty(sPath))
                return;

            //Create a new FileSystemWatcher.
            FileSystemWatcher watcher = new FileSystemWatcher();

            watcher.Path = sPath;
            watcher.Filter = sFilter;

            watcher.Changed += fileSystemEventHandler;
            watcher.Deleted += fileSystemEventHandler;
            watcher.Created += fileSystemEventHandler;
            if (renamedEventHandler != null)
            {
                watcher.Renamed += renamedEventHandler;
            }

            //Enable the FileSystemWatcher events.
            watcher.EnableRaisingEvents = true;
        }
    }
}
